package com.macro.mall.controller.ums;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.api.exception.BusinessException;
import com.macro.mall.domian.ums.WithdrawExport;
import com.macro.mall.dto.ums.PagedQueryMemberWithdrawLogReqDto;
import com.macro.mall.dto.ums.WithdrawSuccessReqDto;
import com.macro.mall.ums.domain.MemberWithdrawLog;
import com.macro.mall.ums.entity.MemberWithdrawLogEntity;
import com.macro.mall.ums.enums.MemberWithdrawLogExportStatusEnum;
import com.macro.mall.ums.enums.MemberWithdrawLogStatusEnum;
import com.macro.mall.ums.service.withdraw.MemberWithdrawLogService;
import com.macro.mall.ums.service.withdraw.bo.PagedQueryMemberWithdrawLogInput;
import com.macro.mall.util.FileWithExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;


/**
 * 会员提现记录控制器
 *
 * @author xuyanjun
 * @date 2019-12-28
 */
@Api(tags = "会员提现记录")
@Slf4j(topic = "MemberWithdrawLogController")
@RestController
@RequestMapping("/memberWithdrawLog")
public class MemberWithdrawLogController {

    @Autowired
    private MemberWithdrawLogService memberWithdrawLogService;

    @PostMapping("export")
    @ApiOperation("导出提现列表")
    public void export() {
        List<MemberWithdrawLogEntity> memberWithdrawLogEntities = memberWithdrawLogService.listByStatusAndExportStatus(MemberWithdrawLogStatusEnum.CONDUCT, MemberWithdrawLogExportStatusEnum.NOT_EXPORT);
        List<MemberWithdrawLog> memberWithdrawLogs = memberWithdrawLogService.entityToDomain(memberWithdrawLogEntities);
        if (memberWithdrawLogs.isEmpty()) {
            throw new BusinessException("没有需要导出的提现记录。");
        }

        List<WithdrawExport> withdrawExports = new ArrayList<>();
        memberWithdrawLogs.forEach(x -> {
            WithdrawExport withdrawExport = new WithdrawExport();

            BeanUtil.copyProperties(withdrawExport, x);

            withdrawExports.add(withdrawExport);
        });

        HttpServletResponse response = getResponse();

        String fileName = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS")) + "提现表.xls";
        FileWithExcelUtil.exportExcel(withdrawExports, "提现信息表", "提现表", WithdrawExport.class, fileName, response);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

        memberWithdrawLogEntities.forEach(x ->
                x.setAmount(null).setExportStatus(1));

        memberWithdrawLogService.saveOrUpdateBatch(memberWithdrawLogEntities);

    }

    @PostMapping("withdrawSuccess")
    @ApiOperation("提现成功")
    public CommonResult withdrawSuccess(@RequestBody WithdrawSuccessReqDto reqDto)
    {
        List<MemberWithdrawLogEntity> memberWithdrawLogEntities = memberWithdrawLogService.listByBatchIds(reqDto.getWithdrawLogIds());
        memberWithdrawLogEntities.forEach(x->x.setStatus(1).setAmount(null));

        memberWithdrawLogService.saveOrUpdateBatch(memberWithdrawLogEntities);

        return CommonResult.success(null);
    }

    /**
     * 分页查询会员提现记录
     *
     * @return 分页后的会员提现记录
     */
    @ApiOperation("分页查询会员提现记录")
    @PostMapping("pagedQuery")
    public CommonPage<MemberWithdrawLog> pagedQuery(@RequestBody PagedQueryMemberWithdrawLogReqDto reqDto) {
        PagedQueryMemberWithdrawLogInput pagedQueryMemberWithdrawLogInput = new PagedQueryMemberWithdrawLogInput();
        pagedQueryMemberWithdrawLogInput
                .setPageIndex(reqDto.getPageIndex())
                .setPageSize(reqDto.getPageSize());

        PageInfo<MemberWithdrawLog> pageInfo = memberWithdrawLogService.pagedQuery(pagedQueryMemberWithdrawLogInput);

        return CommonPage.restPage(pageInfo);
    }

    /**
     * 获取当前httpRequest
     *
     * @return
     */
    public static HttpServletRequest getRequest() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        if (requestAttributes != null) {
            return requestAttributes.getRequest();
        }
        return null;
    }

    /**
     * 获取当前httpResponse
     *
     * @return
     */
    public static HttpServletResponse getResponse() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        if (requestAttributes != null) {
            return requestAttributes.getResponse();
        }
        return null;
    }

}

